//
//  AdetailViewController.m
//  ZhiBoYuan
//
//  Created by apple on 2018/4/17.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "AdetailViewController.h"

#import "KeChengDaGangViewController.h"
#import "KeChengXiangQingVC.h"

#import "KeChengPingLunVC.h"
#import "SuiTangLianXiVC.h"


#import "ChongZhiViewController.h"

//#define ImageHight 250

@interface AdetailViewController ()<UIAlertViewDelegate>
{
    
    CKSlideMenu *slideMenu;
    
    CGFloat ImageHight  ;
}
@property (nonatomic,strong)UIImageView *headImage;


@property (nonatomic,strong)UIButton *buttonA;

@property (nonatomic,strong)UIButton *buttonB;

@property (nonatomic, assign)BOOL xuedouIsGou;//学豆够不够

@property (nonatomic, assign)int price;

@end

@implementation AdetailViewController
{
    KeChengDaGangViewController *daGangVc;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    ImageHight  =  screenWidth/16 * 9;
    
    [self setupConfigureView];
    
    //    [self downloadCourseDetailDatas];
    
    [self loadKeData];
    
}


-(void)loadKeData{
    
    
    SGParameter *parameter  = [[SGParameter alloc] init];
    parameter.body = @{
                       @"course_id":self.course_id// 1 直播 2微课 3套餐
                       };
    
    [SGHttpTool POST:[NSString stringWithFormat:@"%@g=Client&m=Course&a=miniCourseInfo",HTTPP] parameter:parameter sucess:^(id responseObject) {
        NSLog(@"%@",responseObject);
        
        NSDictionary *dic = responseObject;
        
        if ([dic[@"status"] intValue] == 200) {
            
            [_headImage sd_setImageWithURL:[NSURL URLWithString:[dic[@"data"][@"course_info"] valueForKey:@"image_url"]] placeholderImage:[UIImage imageNamed:@"lessonPlace"]];
            
            _price = [[dic[@"data"][@"course_info"] valueForKey:@"price"] intValue];
            
            if ([[dic[@"data"] valueForKey:@"had_collect"]intValue] == 1) {
                //已收藏
                [_buttonA setTitle:@"取消收藏" forState:0];
            }else{
                [_buttonA setTitle:@"加入收藏" forState:0];
            }
            
            if (Teacher) {
                [_buttonB setTitle:@"进入学习" forState:0];
            }else{
                if ([[dic[@"data"] valueForKey:@"had_buy"]intValue] == 1) {
                    //已购买
                    [_buttonB setTitle:@"进入学习" forState:0];
                }else{
                    [_buttonB setTitle:@"加入学习" forState:0];
                }
                
            }
            
            
            [MBProgressHUD hideHUDForView:self.view animated:YES];
            
        }else{
            [MBProgressHUD showTextOnly:self.view message:dic[@"msg"] delay:2.0f];
        }
        
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
        
    }];
    
}

- (void)downloadCourseDetailDatas{
    
    SGParameter *parameter  = [[SGParameter alloc] init];
    parameter.body = @{
                       @"course_id":self.course_id// 1 直播 2微课 3套餐
                       };
    [SGHttpTool POST:Host_Append(CourseList) parameter:parameter sucess:^(id responseObject) {
        //成功回调
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:responseObject options:NSJSONWritingPrettyPrinted error:nil];
        NSString *resultStr =[[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
        ZYLog(@"%@",resultStr);
        //        SGReceive *receive = [SGReceive receiveWithResponseObject:responseObject dataWord:@"course_list" dataClass:[MicroCourseModel class]];
        //        [self.dataArray addObjectsFromArray:receive.data];
        //        [self.tableView reloadData];
        
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
        
    }];
    
}

- (void)setupConfigureView {
    
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.headImage];
    _headImage.sd_layout
    .topSpaceToView(self.view, 0)
    .leftSpaceToView(self.view, 0)
    .rightSpaceToView(self.view, 0)
    .heightIs(ImageHight);
    
    
    
    [self.view addSubview:self.buttonA];
    CGFloat bot = 0;
    if (iPhoneX) { bot = 24; }
    _buttonA.sd_layout
    .leftSpaceToView(self.view, 0)
    .bottomSpaceToView(self.view, bot)
    .widthIs(ScreenWidth/2)
    .heightIs(50);
    
    
    [self.view addSubview:self.buttonB];
    _buttonB.sd_layout
    .rightSpaceToView(self.view, 0)
    .bottomSpaceToView(self.view, bot)
    .widthIs(ScreenWidth/2)
    .heightIs(50);
    
    [self addsliderMenu];
    
    
    UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"分享-3"] style:(UIBarButtonItemStyleDone) target:self action: @selector(shere)];
    
    self.navigationItem.rightBarButtonItems= @[item];
    
    
    
}



-(void)addsliderMenu {
    
    NSArray *titles = @[@"课程大纲",@"课程详情",@"课程评论",@"随堂练习"];
    NSMutableArray *arr = [NSMutableArray array];
    daGangVc = [[KeChengDaGangViewController alloc]initWithNibName:@"KeChengDaGangViewController" bundle:nil];
    daGangVc.course_id = self.course_id;
    [arr addObject:daGangVc];
    KeChengXiangQingVC *Bvc =[[KeChengXiangQingVC alloc]initWithNibName:@"KeChengXiangQingVC" bundle:nil];
    Bvc.course_id = self.course_id;
    [arr addObject:Bvc];
    KeChengPingLunVC *CVC =[[KeChengPingLunVC alloc]initWithNibName:@"KeChengPingLunVC" bundle:nil];
    CVC.course_id = self.course_id;
    [arr addObject:CVC];
    SuiTangLianXiVC *DVC =[[SuiTangLianXiVC alloc]initWithNibName:@"SuiTangLianXiVC" bundle:nil];
    DVC.course_id = self.course_id;
    [arr addObject:DVC];
    
    
    slideMenu = [[CKSlideMenu alloc]initWithFrame:CGRectMake(0, ImageHight,ScreenWidth, HEIGHT)  titles:titles controllers:arr];
    
    CGFloat space = Screenheight - HEIGHT-50-ImageHight;
    
    if (iPhoneX) {
        space = Screenheight - HEIGHT-50-ImageHight-42;
    }
    
    slideMenu.bodyFrame = CGRectMake(0, ImageHight + NAVIGATION_BAR_HEIGHT -20, ScreenWidth, space);
    
    slideMenu.bodySuperView = self.view;
    slideMenu.indicatorOffsety = 2;
    slideMenu.indicatorWidth = ScreenWidth/4-5;
    
    slideMenu.font = [UIFont systemFontOfSize:17];
    slideMenu.titleStyle     = SlideMenuTitleStyleGradient;
    slideMenu.indicatorStyle = SlideMenuIndicatorStyleNormal;
    
    slideMenu.isFixed = YES;
    slideMenu.showLine = YES;
    
    slideMenu.selectedColor = ZhuTiColor;
    slideMenu.indicatorColor = ZhuTiColor;
    
    slideMenu.unselectedColor = [UIColor grayColor];
    [self.view addSubview:slideMenu];
    
    //    slideMenu.sd_layout
    //    .topSpaceToView(_headImage, 0)
    //    .leftSpaceToView(self.view, 0)
    //    .rightSpaceToView(self.view, 0)
    //    .heightIs(HEIGHT);
    
}

-(void)saveSomth{
    
    if ([_buttonA.titleLabel.text isEqualToString:@"加入收藏"]) {
        //加入收藏
        SGParameter *parameter  = [[SGParameter alloc] init];
        parameter.body = @{
                           @"course_id":self.course_id// 1 直播 2微课 3套餐
                           };
        
        [SGHttpTool POST:[NSString stringWithFormat:@"%@g=Client&m=Collect&a=addCourse",HTTPP] parameter:parameter sucess:^(id responseObject) {
            NSLog(@"%@",responseObject);
            
            NSDictionary *dic = responseObject;
            
            if ([dic[@"status"] intValue] == 200) {
                [MBProgressHUD showTextOnly:self.view message:@"收藏成功" delay:2.0f];
                [_buttonA setTitle:@"取消收藏" forState:0];
                
            }else{
                [MBProgressHUD showTextOnly:self.view message:dic[@"msg"] delay:2.0f];
            }
            
        } failure:^(NSError *error) {
            NSLog(@"%@",error);
            
        }];
        
    }else{
        //取消收藏
        SGParameter *parameter  = [[SGParameter alloc] init];
        parameter.body = @{@"course_id":self.course_id,
                           };
        
        [SGHttpTool POST:[NSString stringWithFormat:@"%@g=Client&m=Collect&a=delCourse",HTTPP] parameter:parameter sucess:^(id responseObject) {
            NSLog(@"%@",responseObject);
            NSDictionary *dic = responseObject;
            
            if ([dic[@"status"] intValue] == 200) {
                [MBProgressHUD showTextOnly:self.view message:dic[@"msg"] delay:2.0f];
                
                [_buttonA setTitle:@"加入收藏" forState:0];
                
            }else{
                [MBProgressHUD showTextOnly:self.view message:dic[@"msg"] delay:2.0f];
            }
            
        } failure:^(NSError *error) {
            NSLog(@"%@",error);
            
        }];
        
    }
    
    
    
    
}


//加入学习
-(void)addToStudyAction{
    
    if (Teacher) {
        //学习
        [daGangVc startLearning];
    }else{
        
        if ([_buttonB.titleLabel.text isEqualToString:@"加入学习"]) {
            
            NSString *totalDou = [ZYUserManager userInfo].left_xuedou;
            int dou = _price;
            
            NSString *message1 = [NSString stringWithFormat:@"您确定使用%d个学豆？",dou];
            
            if ([totalDou intValue] >= dou) {
                _xuedouIsGou = YES;
                UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:message1 delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil, nil];
                [alertView show];
            }else{
                _xuedouIsGou = NO;
                UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"您的学豆不足，是否去充值？" delegate:self cancelButtonTitle:@"否" otherButtonTitles:@"是", nil, nil];
                [alertView show];
            }
            
        }else{
            //学习
            [daGangVc startLearning];
            
        }
        
    }
    
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        
    }else if (buttonIndex == 1){
        
        if (_xuedouIsGou == YES) {
            
            SGParameter *parameter  = [[SGParameter alloc] init];
            NSLog(@"%@",self.course_id);
            parameter.body = @{
                               @"course_id":self.course_id// 1 直播 2微课 3套餐
                               };
            
            [SGHttpTool POST:[NSString stringWithFormat:@"%@g=Client&m=Order&a=buyCourse",HTTPP] parameter:parameter sucess:^(id responseObject) {
                NSLog(@"%@",responseObject);
                
                NSDictionary *dic = responseObject;
                
                if ([dic[@"status"] intValue] == 200) {
                    if ([dic[@"data"][@"status"] intValue] == 1) {
                        [MBProgressHUD showTextOnly:self.view message:@"加入成功" delay:2.0f];
                        [_buttonB setTitle:@"进入学习" forState:0];
                        
                    }else{
                        [MBProgressHUD showTextOnly:self.view message:dic[@"data"][@"msg"] delay:2.0f];
                        
                    }
                    
                }else{
                    [MBProgressHUD showTextOnly:self.view message:dic[@"msg"] delay:2.0f];
                }
                
            } failure:^(NSError *error) {
                NSLog(@"%@",error);
                
            }];
        }else{
            ChongZhiViewController *chongVC = [[ChongZhiViewController alloc]init];
            [self.navigationController pushViewController:chongVC animated:YES];
            
        }
        
    }
    
}




-(void)shere{
    NSLog(@"分享");
}


-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [Helper setMoRenNav:self.navigationController];
    [Helper setNavColorFrom:self.navigationController color:[UIColor clearColor]];
    
    [IQKeyboardManager sharedManager].enable = NO;
    
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    [Helper setMoRenNav:self.navigationController];
    [Helper setNavColorFrom:self.navigationController color:ZhuTiColor];
    
    [IQKeyboardManager sharedManager].enable = YES;
    
}


-(UIImageView *)headImage{
    if(!_headImage){
        _headImage= [UIImageView new];
        
        _headImage.image = [UIImage imageNamed:@"lessonPlace"];
    }
    return _headImage;
}

-(UIButton *)buttonA{
    if (!_buttonA) {
        _buttonA = [UIButton buttonWithType:(UIButtonTypeCustom)];
        
        [_buttonA setBackgroundColor:[UIColor whiteColor]  ];
        
        [_buttonA setTitle:@"加入收藏" forState:0];
        
        [_buttonA addTarget:self action:@selector(saveSomth) forControlEvents:(UIControlEventTouchUpInside)];
        
        [_buttonA setTitleColor:ZhuTiColor forState:(UIControlStateNormal)];
        
        _buttonA.layer.borderColor = ZhuTiColor.CGColor;
        
        _buttonA.layer.borderWidth = 1;
        
    }
    return _buttonA;
}

-(UIButton *)buttonB{
    if (!_buttonB) {
        _buttonB = [UIButton buttonWithType:(UIButtonTypeCustom)];
        
        [_buttonB setBackgroundColor:ZhuTiColor];
        
        [_buttonB setTitle:@"加入学习" forState:0];
        
        [_buttonB addTarget:self action:@selector(addToStudyAction) forControlEvents:(UIControlEventTouchUpInside)];
        
    }
    return _buttonB;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end

